<?php
$title = "NJU网上商城 - 首页";
$css = "index.css";
include_once 'common/top.php';
include_once '../Class/DB.php';
$db=new DB();
$best_sold=$db->get_best_sold(8);//获取卖的最多的8个商品
$newly_p=$db->get_newly(8);//获得最新的8个商品
$announcement=$db->get_desc_announcement();//获取按日期新旧排序的公告
$messagePerPage=6;//留言板每页显示数量
$announce_num=6;//公告板显示最新公告的数量
$totalPage=ceil($db->get_pid_messageNum(0)/$messagePerPage); //计算出总页数
$page=$_GET['page']; //获得当前的页面值
if (!isset($page)) {
 $page=1;
} //如果没有值,则赋值1
$startCount=($page-1)*$messagePerPage; //分页开始,根据此方法计算出开始的记录
$messages=$db->get_shop_message($startCount, $messagePerPage);//获取对商店的评价
$recommends=$db->get_recommend();
?>
<div id="recommend">
	<script src="../js/index.js" type="text/javascript"></script>
	<div id="recommend_img">
		<div id="recommend_img_container">
		<?php $i=0;
		while(isset($recommends[$i]))
		{
		?>
			<a href="item.php?id=<?php echo $recommends[$i]["pid"];?>">
				<img alt="热销商品" src="<?php echo $recommends[$i]["picurl"];?>">
			</a>
			<?php 
			$i++;
		}
			?>
		</div>
	</div>
	<div id="recommend_tab">
		<ul id="recommend_tab_ul">
		<li id="recommend_tab_li_0" onclick="javascript:setrecommendTab(0);" class="current"><p>1</p></li>
		<?php 
		$i=1;
		while(isset($recommends[$i]))
		{
		?>
			<li id="recommend_tab_li_<?php echo $i?>" onclick="javascript:setrecommendTab(<?php echo $i;?>);"><p><?php echo $i+1;?></p></li>
		<?php 
		$i++;
		}
		?>
		</ul>
	</div>
</div>

<div id="layout_0">
	<div id="layout_0_0">
		<div id="announcement" class="block_mode">
			<h3>公告</h3>
			<div class="block_context">
			<?php 
			$i=0;
			$n=$announce_num;
			while(isset($announcement[$i]))
			{
			?>
			<a href="announcement.php?id=<?php echo $announcement[$i]["aid"]?>">[<?php echo substr($announcement[$i]["date"],0,4)."-".substr($announcement[$i]["date"],4,2)."-".substr($announcement[$i]["date"],6,2);?>] 公告<?php echo ($i+1);?>---<?php echo $announcement[$i]["a_name"];?></a>
		    <?php
		    $i++;
		    $n--;
		    if($n==0)
		    break; 
		    
			}
		    ?>	
			</div>
		</div>
		<div id="classify" class="block_mode">
			<h3>分类<a class="more" href="classify.php">更多</a></h3>
			
			<div class="block_context">
				<ul>
				<?php 
               $name=$db->get_parent_types(0);
               $i=0;
				while(isset($name[$i]))
				{
				
				?>
					<li>
						<dl>
            				<dt><?php echo $name[$i]["name"];?></dt>
            				<?php 
            				$classes=$db->get_parent_types($name[$i]["tid"]);
            				$j=0;
            				while($classes[$j])
            				{
            				?>
            				<dd><a href="search.php?key=&classify1=<?php echo $name[$i]["name"];?>&classify2=<?php echo $classes[$j]["name"];?>"><?php echo $classes[$j]["name"];?></a></dd>
            				<?php 
            				$j++;
            				}
            				?>
            			</dl>
					</li>
					<?php 
				$i++;
				}
					?>
				</ul>
			</div>
		</div>
	</div>
	<div id="layout_0_1">
		<div id="new" class="block_mode">
			<h3>新品上市</h3>
			<div class="block_context">
			<?php 
			$i=0;
			while(isset($newly_p[$i]))
			{
			?>
			<div class="adv">
					<a href="item.php?id=<?php echo $newly_p[$i]["pid"];?>"><img alt="商品" src="<?php echo $newly_p[$i]["pic_url"];?>"></a>
					<h4><a href="item.php?id=<?php echo $newly_p[$i]["pid"];?>"><?php echo $newly_p[$i]["p_name"];?></a></h4>
					<p class="no">￥<?php echo $newly_p[$i]["ori_price"];?></p>
					<p>￥<?php echo $newly_p[$i]["act_price"];?></p>
				</div>	
			<?php 
				$i++;
			}
			?>
			</div>
		</div>
		
		<div id="hot" class="block_mode">
			<h3>热销商品</h3>
			<div class="block_context">
			<?php 
			$i=0;
			while(isset($best_sold[$i]))
			{
			?>
				<div class="adv">
					<a href="item.php?id=<?php echo $best_sold[$i]["pid"];?>"><img alt="商品" src="<?php echo $best_sold[$i]["pic_url"];?>"></a>
					<h4><a href="item.php?id=<?php echo $best_sold[$i]["pid"];?>"><?php echo $best_sold[$i]["p_name"];?></a></h4>
					<p>累计售出<b><?php echo $db->get_pid_sold($best_sold[$i]["pid"]);?></b>件</p>
				</div>
			<?php 
			$i++;
			}
			?>				
			</div>
		</div>
	</div>
	<div class="clearfix"></div>
</div>


<div id="message" class="block_mode">
	<h3>留言板</h3>
	<div class="block_context">
		<div id="message_leave">
		<?php 
		$i=0;
		$n=$messagePerPage;
		while(isset($messages[$i]))
		{
			$user_info=$db->get_uid_info($messages[$i]["uid"]);
			$messages[$i]["content"]=str_replace("\r\n", "<br/>", $messages[$i]["content"]);
			$messages[$i]["content"]=str_replace("\r", "<br/>", $messages[$i]["content"]);
			$messages[$i]["content"]=str_replace("\n", "<br>", $messages[$i]["content"]);
		?>
			<div class="msg">
				<h4><?php echo $user_info[0]["userName"];?></h4>
				<p><?php echo $messages[$i]["content"];?></p>
				<div class="end">
					<p><?php echo substr($messages[$i]["date"],0,4)."-".substr($messages[$i]["date"],4,2)."-".substr($messages[$i]["date"],6,2)." ".substr($messages[$i]["date"],8,2).":".substr($messages[$i]["date"],10,2);?></p>
				</div>
			</div>
		<?php 
		$n--;
		if($n==0)
		break;
		$i++;
		}
		?>
			<div id="page">
			<?php if ($page != 1) { //页数不等于1?>
			<a href="index.php?page=<?php echo $page - 1;?>">上一页</a> <!--显示上一页-->
			<?php }
			for ($i=1;$i<=$totalPage;$i++) {  //循环显示出页面
				if($i==$page){
					?>
					<p><?php echo $i;?></p>
			<?php continue;}
			?>
			<a href="index.php?page=<?php echo $i;?>"><?php echo $i ;?></a>
			<?php
			}
			if ($page<$totalPage) { //如果page小于总页数,显示下一页链接
			?>
				<a href="index.php?page=<?php echo $page + 1;?>">下一页</a>
			<?php
					} 
			?>
			
			</div>
		</div>
	
		<?php 
		if($_SESSION["Passed"]){//如果登录
		?>
		<div id="message_enter">
			<form action="message.php" method="post">
				<textarea name="msg" ></textarea>
				<input type="submit" value="留言" />
				<p>Ctrl+Enter快速提交</p>
			</form>
		</div>
		<?php 
		}
		?>
	</div>
</div>


<?php
include_once 'common/tail.php';
?>